package com.doucat.support.fast.table;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.doucat.support.component.fragment.base.BaseFragment;
import com.doucat.support.component.recycle.CommonRecyclerView;
import com.doucat.support.io.AssetReader;
import com.google.gson.Gson;
import com.tonly.doucatlibrary.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备管理
 * 维修保养
 * 生产统计
 * 人员管理
 * 数据统计
 * 数据分析
 * 信贷服务
 * 系统管理
 * 车队统计
 */
public class ModelFragment extends BaseFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    private CommonRecyclerView lvTable;
    private TableAdapter adapter;
    @Override
    public void onRootViewCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setBaseFragmentContentView(R.layout.fragment_model);
        lvTable=findViewById(R.id.lvTable);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
        lvTable.setLayoutManager(linearLayoutManager);
        int space = getResources().getDimensionPixelOffset(R.dimen.x10);
        lvTable.addItemDecoration(new SpacesItemDecoration(space));
        onRefresh();
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        showLoading();
        runOnThread(new Runnable() {
            @Override
            public void run() {
                List<WorkGroup> groups=new ArrayList<>();
                AssetReader assetReader=new AssetReader(getContext());
                WorkTable table=new Gson().fromJson(assetReader.getJsonFromFile(onLoadJson()),WorkTable.class);
                for(int i=0;i<table.data.size();i++){
                    WorkGroup group=table.data.get(i);
                    if(group.enable){
                        groups.add(group);
                    }
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter=new TableAdapter(getContext(),groups);
                        lvTable.setAdapter(adapter);
                        dismissLoading();
                    }
                });
            }
        });
    }

    public String onLoadJson(){
        return "";
    }

    class SpacesItemDecoration extends RecyclerView.ItemDecoration {
        private int space;

        public SpacesItemDecoration(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view,
                                   RecyclerView parent, RecyclerView.State state) {
            // Add top margin only for the first item to avoid double space between items
            if (parent.getChildLayoutPosition(view)!= 0) outRect.top = space;
        }
    }
}
